package com.app.diary.data.impl;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class LocationService {

    private static LocationService instance;
    private final AMapLocationClient mLocationClient;
    private OnCityReceivedListener mListener;
    private final Context mContext;

    public interface OnCityReceivedListener {
        void onCityReceived(String city);
        void onErrorOccurred(String errorMessage);
    }

    private LocationService(Context context) throws Exception {
        this.mContext = context.getApplicationContext();
        mLocationClient = new AMapLocationClient(mContext);
        initLocation();
        mLocationClient.setLocationListener(new MyLocationListener());
    }

    public static synchronized LocationService getInstance(Context context) throws Exception {
        if (instance == null) {
            instance = new LocationService(context);
        }
        return instance;
    }

    public void setListener(OnCityReceivedListener listener) {
        this.mListener = listener;
    }

    public void start() {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // 如果没有权限，请在此处请求权限
            if (mListener != null) {
                mListener.onErrorOccurred("Location permissions not granted");
            }
            return;
        }
        mLocationClient.startLocation();
    }

    public void stop() {
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
        }
    }

    private void initLocation() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setNeedAddress(true); // 设置是否返回地址信息
        option.setOnceLocation(true); // 设置为仅请求一次定位
        mLocationClient.setLocationOption(option);
    }

    private class MyLocationListener implements AMapLocationListener {
        @Override
        public void onLocationChanged(AMapLocation location) {
            if (location == null || mListener == null) {
                return;
            }

            if (location.getErrorCode() != 0) {
                mListener.onErrorOccurred("Location Error, ErrCode:" + location.getErrorCode() + ", errInfo:" + location.getErrorInfo());
                return;
            }

            String city = location.getCity().replaceAll("市", ""); // 去除“市”字
            mListener.onCityReceived(city);

            // 单次定位后停止定位
            stop();
        }
    }
}